package com.five.controller;

import com.alibaba.fastjson.JSON;
import com.five.entity.BusinessActivities;
import com.five.service.BusinessActivitiesService;
import com.five.service.FileGroupService;
import com.five.service.UserBusinessActivitiesService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zero on 2018/1/28.
 */
@RestController
@RequestMapping("/youxue/index")
public class BusinessActivitiesController {

    private static final Logger looger = Logger.getLogger(BusinessActivitiesController.class);

    @Autowired
    private BusinessActivitiesService businessActivitiesService;
    @Autowired
    private UserBusinessActivitiesService userBusinessActivitiesService;
    @Autowired
    private FileGroupService fileGroupService;


    /**
     * 查找不同类型的商家活动信息
     * @return
     */
    @RequestMapping(value = "/activities/{activitiesType}",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String getCampActivities(@PathVariable String activitiesType){

        Map<String,Object> map= new HashMap<String, Object>();
        List<BusinessActivities> businessActivities=businessActivitiesService.findBusinessActivitiesByActivitiesType(activitiesType);
        int[] activitiesNumbers=new int[businessActivities.size()];
        if(businessActivities!=null){
            for(int i=0;i<businessActivities.size();i++){
                activitiesNumbers[i]=userBusinessActivitiesService.findGroupPersonNumber(businessActivities.get(i).getId());
                String img=fileGroupService.findFileGroupById(businessActivities.get(i).getActivityFileGroupId()).getGroupBaseUrl();
                businessActivities.get(i).setActivityImg(img);
            }
            map.put("businessActivities",businessActivities);
            map.put("activitiesNumbers",activitiesNumbers);
            return JSON.toJSON(map).toString();
        }else {
            return "error";
        }
    }


    /**
     * 添加活动
     * @param businessActivities
     * @return
     */
    @RequestMapping(value = "/activities/add",method = RequestMethod.POST)
    public String addActivities(@RequestBody BusinessActivities businessActivities) {

        if(businessActivities!=null){
            businessActivitiesService.addBusinessActivities(businessActivities);
            return "add success";
        }else {
            return "error";
        }
    }


    /**
     * 通过名字查找
     * @return
     */
    @RequestMapping(value = "/activitiesName/{activitiesName}",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String getActivitiesByName(@PathVariable String activitiesName){


        List<BusinessActivities> businessActivities=businessActivitiesService.findActivitiesByName(activitiesName);
        if(businessActivities!=null){
            for(int i=0;i<businessActivities.size();i++){
                String img=fileGroupService.findFileGroupById(businessActivities.get(i).getActivityFileGroupId()).getGroupBaseUrl();
                businessActivities.get(i).setActivityImg(img);
            }
            return JSON.toJSON(businessActivities).toString();
        }else {
            return "error";
        }
    }


    /**
     * 查询单个活动详情
     * @return
     */
    @RequestMapping(value = "/activities/select/{activitiesId}",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String selectBusinessActivities(@PathVariable int activitiesId) {


        BusinessActivities businessActivities=businessActivitiesService.findActivitiesById(activitiesId);
        if (businessActivities != null) {

            String img=fileGroupService.findFileGroupById(businessActivities.getActivityFileGroupId()).getGroupBaseUrl();
            businessActivities.setActivityImg(img);

            return JSON.toJSON(businessActivities).toString();
        } else {
            return "error";
        }

    }



//    /**
//     * 快速参团
//     * @return
//     */
//    @RequestMapping(value = "/activities/show",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
//    public String getNoOpenGroupActivities(){
//
//        List<BusinessActivities> businessActivities=businessActivitiesService.findNoOpenGroupActivities(0);
//        List<List<File>> filesList= new ArrayList<List<File>>();
//        List<BusinessActivities> newBusinessActivities=new ArrayList<BusinessActivities>();
//        if (businessActivities !=null){
//            int nowNumber=0;
//            int number1=0;
//            int number2=0;
//            for(int i=0;i<businessActivities.size();i++){
//
//                List<File> files=fileService.selectFiles(businessActivities.get(i).getActivityFileGroupId());
//                filesList.add(files);
//
//                number1=userBusinessActivitiesService.findGroupPersonNumber(businessActivities.get(i).getId());
//                number2=businessActivities.get(i).getActivityNumber();
//                nowNumber=number2-number1;
//                if(nowNumber<=5){
//                    newBusinessActivities.add(businessActivities.get(i));
//                }else if(nowNumber<=15&&nowNumber>5){
//                    newBusinessActivities.add(businessActivities.get(i));
//                }else {
//                    newBusinessActivities.add(businessActivities.get(i));
//                }
//
//            }
//            return JSON.toJSON(newBusinessActivities).toString();
//        }else {
//            return null;
//        }
//    }

    /**
     * 获取最新活动
     * @return
     */
    @RequestMapping(value = "/activities/newest",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String getNewActivities(){

        Map<String,Object> map= new HashMap<String, Object>();
        List<BusinessActivities> businessActivities=businessActivitiesService.findNewBActivitiesByNumber(0, 10);
        int[] activitiesNumbers=new int[businessActivities.size()];
        if(businessActivities!=null){
            for(int i=0;i<businessActivities.size();i++){
                activitiesNumbers[i]=userBusinessActivitiesService.findGroupPersonNumber(businessActivities.get(i).getId());
                String img=fileGroupService.findFileGroupById(businessActivities.get(i).getActivityFileGroupId()).getGroupBaseUrl();
                businessActivities.get(i).setActivityImg(img);
            }
            map.put("businessActivities",businessActivities);
            map.put("activitiesNumbers",activitiesNumbers);
            return JSON.toJSON(map).toString();

        }else {
            return null;
        }
    }


}
